package com.bjc.platform.export;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.j2ee.servlets.ImageServlet;

import com.bjc.platform.exception.JasperReportException;
import com.bjc.platform.vo.LineChartVO;

/**
 * 利用报表生成HTML格式报表
 */
public class HTMLExport {

	/**
	 * 导出报表
	 * 
	 * @param request
	 * @param response
	 * @param reportFilePath
	 * @param params
	 * @param con
	 * @throws JasperReportException
	 */
	public void export(HttpServletRequest request,
			HttpServletResponse response, String reportFilePath, Map params,
			Connection con) throws JasperReportException {
		try {
			PrintWriter out = response.getWriter();
			try {
				response.setContentType("text/html;charset=UTF-8");
				JasperPrint jasperPrint = new JasperPrintWithConnection(
						reportFilePath, params, con).getJasperPrint();
				// 使用JRHtmlExproter导出Html格式
				JRHtmlExporter exporter = new JRHtmlExporter();
				request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE,jasperPrint);
				exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
				exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
				String imageDir = new File(reportFilePath).getAbsolutePath();
				exporter.setParameter(JRHtmlExporterParameter.IMAGES_DIR_NAME, request.getRealPath("")+"/report/image"); 
				exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,"rest/wineWarehosue/getReportImg?img=");
				exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE); 
				exporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, Boolean.TRUE); 
				exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
				// 导出
				exporter.exportReport();
			} catch (Exception e) {
				e.printStackTrace();
				throw new JasperReportException("在导出Html格式报表时发生错误!");
			} finally {
				if (out != null) {
					try {
						out.close();
					} catch (Exception e) {
					}
				}
			}
		} catch (IOException ioe) {
			ioe.printStackTrace();
			throw new JasperReportException("从Response中取得PrintWriter时发生错误!");
		}
	}
	/**
	 * 导出报表
	 * 
	 * @param request
	 * @param response
	 * @param reportFilePath
	 * @param params
	 * @param con
	 * @throws JasperReportException
	 */
	public void exportLineChartByEntity(HttpServletRequest request,
			HttpServletResponse response, String reportFilePath, Map params,
			List<LineChartVO> vo) throws JasperReportException {
		try {
			PrintWriter out = response.getWriter();
			try {
				// 报表生成
				JRDataSource dataSource = new JRBeanCollectionDataSource(vo);
				JasperPrint jasperPrint = JasperFillManager.fillReport(reportFilePath, params, dataSource);
				
				// 使用JRHtmlExproter导出Html格式
				response.setContentType("text/html;charset=UTF-8");
				JRHtmlExporter exporter = new JRHtmlExporter();
				request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE,jasperPrint);
				exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
				exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
				String imageDir = new File(reportFilePath).getAbsolutePath();
				exporter.setParameter(JRHtmlExporterParameter.IMAGES_DIR_NAME, request.getRealPath("")+"/report/image"); 
				exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,"report/image/");
				exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE); 
				exporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, Boolean.TRUE); 
				exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
				// 导出
				exporter.exportReport();
			} catch (Exception e) {
				e.printStackTrace();
				throw new JasperReportException("在导出Html格式报表时发生错误!");
			} finally {
				if (out != null) {
					try {
						out.close();
					} catch (Exception e) {
					}
				}
			}
		} catch (IOException ioe) {
			ioe.printStackTrace();
			throw new JasperReportException("从Response中取得PrintWriter时发生错误!");
		}
	}
}